package com.aisb.manage;

import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description：
 * @Author wucm
 * @Date 2017/12/28
 */
@Service
public class FuncService {
    private static Logger logger = LoggerFactory.getLogger(FuncService.class);

    @Autowired
    FuncDao funcDao;

    public Object funcOne(String funcId) {
        return funcDao.queryOne(funcId);
    }

    public Object funcOne(Map param) {
        return funcDao.queryOne(param);
    }

    public List queryFolder(){
        return funcDao.queryFolder();
    }

    public List funcList(Map param) {
        List<Map> funcList = funcDao.queryList(param);

        return funcList;
    }

    public Page funcList(Map param, Integer pageNum, Integer pageSize) {
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize) {
            pageSize = 999999;
        }

        return funcDao.searchPage(param, pageNum, pageSize);
    }

    public int funcAdd(Map param) {
        return funcDao.insert(param);
    }

    @Transactional
    public int funcModify(Map modifyInfo) {
        try {
            funcDao.update(modifyInfo);
        } catch (Exception e) {
            logger.error("{}", e);
            return 0;
        }

        return 1;
    }

    @Transactional
    public int funcModify(Set<String> funcIds, Map modifyInfo) {
        try {
            for (String funcId : funcIds) {
                if (StringUtils.isBlank(funcId)) {
                    continue;
                }
                modifyInfo.put("funcId", funcId);
                funcDao.update(modifyInfo);
            }
        } catch (Exception e) {
            logger.error("{}", e);
            return 0;
        }

        return 1;
    }

    @Transactional
    public int funcDelete(Map param) {
        try {
            funcDao.delete(param);
            funcDao.deleteFuncRole(param);
            funcDao.deleteFuncUser(param);
        } catch (Exception e) {
            logger.error("{}", e);
            return 0;
        }

        return 1;
    }
}
